package com.situ.mybatis.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	private static SqlSessionFactory sqlSessionFactory;
	static {
		// 加载MyBatis 的核心配置文件
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream("mybatis.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 创建SqlSessionFactory:创建SqlSession的工厂 DBFactory
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}

	public static SqlSession getSqlSession() {
		// 创建SqlSession，由会话工厂创建sqlSession即会话，操作数据库需要通过sqlSession进行。
		SqlSession sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
	}
}
